    $(function(){
  
		/* ############## upload file ################ */
		var button = $('.uploadButton'), interval;
		
		new AjaxUpload(button, {
			action: '/cloud/fileUploader/process', 
			name: 'file',
            responseType: 'json',
            data : {
				'upload' : "attachments", 
				'successAction' : "upload",
				'successController' : "coursewareAjax",
				'errorAction' : "upload",
				'errorController' : "coursewareAjax"
			}, 

			onSubmit : function(file, ext){
				// change button text, when user selects file
				$('#errors').html('');
				var Uploading = $("#messages");
					Uploading.text('Uploading');
								
				// If you want to allow uploading only 1 file at time,
				// you can disable upload button
				this.disable();
				
				// Uploding -> Uploading. -> Uploading...
				interval = window.setInterval(function(){
					var text = Uploading.text();
					if (text.length < 13){
						Uploading.text(text + '.');					
					} else {
						Uploading.text('Uploading');				
					}
				}, 200);
			},
			onComplete: function(file, response){ 
				
				window.clearInterval(interval);
				 $("#messages").text('');
				// enable upload button
				this.enable();
			 
                if (response.success)
                {	// add file to the list
                     $('#attachmentList').append("<div class='filename' id='"+response.message.id+"'><a href='javascript:' class='showAttachment'>"+response.message.name + "(" +Math.round(response.message.size/1025)+ "k)"+"</a><a href='javascript:' class='deleteAttachment'>删除附件</a><input type='hidden' name='attachments' value='"+response.message.id+"'></div>");
               
                }else{
                      $('#errors').html("<div class='errors'>"+response.message+"</div>");
                }
			}
		});

		$(".deleteAttachment").live("click", function(){ 
			var parent = $(this).parent(); 

			$.post("/cloud/articleAjax/deleteAttachment",{'id' :  parent.attr('id') , 'articleId' : $("#id").val() },
				function(json){
 					if (json.success) {
						parent.remove();
					}
				},
				'json');  
		});

		$(".showAttachment").live("click", function(){  
			modalDialog("<div style='text-align:center;vertical-align:middle;margin:auto'><img src='/cloud/download/index/"+ $(this).parent().attr('id') +"?errorAction=show&errorController=articleAjax'></div>");
		});
});
